package com.ibiaoke.system.dao;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.system.entity.XcxAdvert;
import com.ibiaoke.system.entity.XcxAdvertExample;
import com.ibiaoke.system.mapper.XcxAdvertMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class XcxAdvertDao{
    @Autowired
    private XcxAdvertMapper xcxAdvertMapper;
    /**
     * 查询Advert
     * @param xcxAdvert
     *@param page  @return
     */
    public List<XcxAdvert> listBanner(XcxAdvert xcxAdvert,List<Integer> shopIds,Page page){
        XcxAdvertExample example = new XcxAdvertExample();
        XcxAdvertExample.Criteria criteria = example.createCriteria().andPositionEqualTo(xcxAdvert.getPosition()).andDelStatusEqualTo(CommonConsts.NO);
        if(xcxAdvert.getShowFlag()!=null) {
            criteria.andShowFlagEqualTo(xcxAdvert.getShowFlag());
        }
        if (shopIds != null) {
            criteria.andShopIdIn(shopIds);
        }
        page.setTotalCount(xcxAdvertMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("sort asc");
        return  xcxAdvertMapper.selectByExample(example);
    }

    public int saveBanner(XcxAdvert xcxAdvert) {
        return xcxAdvertMapper.insert(xcxAdvert);
    }

    public int updateBannerById(XcxAdvert xcxAdvert) {
        XcxAdvertExample example = new XcxAdvertExample();
        example.createCriteria().andIdEqualTo(xcxAdvert.getId());
        return xcxAdvertMapper.updateByExampleSelective(xcxAdvert, example);
    }

    public int deleteBannerById(Integer xcxAdvertId) {
        XcxAdvert advert = new XcxAdvert();
        advert.setId(xcxAdvertId);
        advert.setDelStatus(1);
        return  xcxAdvertMapper.updateByPrimaryKeySelective(advert);
    }

    public int batchDeleteXcxAdver(String xcxAdvertIds) {
        XcxAdvertExample example = new XcxAdvertExample();
        List<Integer> advertIdList = new ArrayList<>();
        AssertUtils.notBlank(xcxAdvertIds,"Banner编号不能为空");

        String[] array = xcxAdvertIds.split(",");
        for (int i = 0; i < array.length; i++) {
            advertIdList.add(Integer.parseInt(array[i]));
        }

        XcxAdvert xcxAdvert = new XcxAdvert();
        xcxAdvert.setDelStatus(CommonConsts.YES);
        xcxAdvert.setUpdateTime(new Date());
        example.createCriteria().andIdIn(advertIdList);

        return xcxAdvertMapper.updateByExampleSelective(xcxAdvert,example);
    }
}
